package com.kadron.item;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.jboss.seam.Component;
import org.jboss.seam.ScopeType;
import org.jboss.seam.annotations.AutoCreate;
import org.jboss.seam.annotations.Create;
import org.jboss.seam.annotations.In;
import org.jboss.seam.annotations.Name;
import org.jboss.seam.annotations.Scope;

import com.kadron.common.KConst;
import com.kadron.dao.KCommonDAO;
import com.kadron.dao.KItemColorDAO;
import com.kadron.dao.KItemMaterialDAO;
import com.kadron.dao.KItemSizeDAO;
import com.kadron.dao.KItemTypeColorDAO;
import com.kadron.dao.KItemTypeMaterialDAO;
import com.kadron.dao.KItemTypeSizeDAO;
import com.kadron.entity.Item;
import com.kadron.entity.ItemColor;
import com.kadron.entity.ItemMaterial;
import com.kadron.entity.ItemSize;
import com.kadron.entity.ItemTypeColor;
import com.kadron.entity.ItemTypeMaterial;
import com.kadron.entity.ItemTypeSize;
import com.kadron.manager.KMessageManager;
import com.kadron.manager.KSelection;

@AutoCreate
@Name("kItemAvalableManager")
@Scope(ScopeType.PAGE)
public class KItemAvalableManager extends KMessageManager implements Serializable {

	/**
	 * 
	 */
	private static final long serialVersionUID = -8898475863804949771L;

	private static final Log log = LogFactory.getLog(KItemAvalableManager.class);
	
	private Item item; 
	
	private List<ItemTypeColor> acolor = new ArrayList<ItemTypeColor>();
	private List<ItemTypeColor> scolor = new LinkedList<ItemTypeColor>();
	private List<ItemTypeSize> asize = new ArrayList<ItemTypeSize>();
	private List<ItemTypeSize> ssize = new LinkedList<ItemTypeSize>();
	private List<ItemTypeMaterial> amaterial = new ArrayList<ItemTypeMaterial>();
	private List<ItemTypeMaterial> smaterial = new LinkedList<ItemTypeMaterial>();
	
	private boolean error;
	
	@In
	private KCommonDAO commonDAO;
	@In
	private KItemColorDAO itemColorDAO;
	@In
	private KItemTypeColorDAO itemTypeColorDAO;
	@In
	private KItemTypeMaterialDAO itemTypeMaterialDAO;
	@In
	private KItemTypeSizeDAO itemTypeSizeDAO;
	@In
	private KItemMaterialDAO itemMaterialDAO;
	@In
	private KItemSizeDAO itemSizeDAO;
	
	@Create
	public void init() {
		try {
			Long itemId = (Long) Component.getInstance("itemId");
			if (itemId == null) {
				throw new NullPointerException("itemId is NULL!");
			}
			loadData(itemId);			
		} catch (Exception e) {
			e.printStackTrace();
			error = true;
		}
	}
	
	private void loadData(long itemId) {
		item = (Item) commonDAO.find(itemId, Item.class);
		scolor = itemTypeColorDAO.getItemColors(itemId);
		ssize = itemTypeSizeDAO.getItemSizes(itemId);
		smaterial = itemTypeMaterialDAO.getItemMaterials(itemId);
		
		asize = itemTypeSizeDAO.getAvailableItemSizes(item);
		acolor = itemTypeColorDAO.getAvailableItemColors(item);
		amaterial = itemTypeMaterialDAO.getAvailableItemMaterials(item);
	}
	
	public void save() {
		if (item == null) {
			bad(KConst.BAD_THING);
		}
		if (log.isDebugEnabled()) {
			log.debug("Save Item itemId="+item.getId());
		}
		
		// COLOR
		for (ItemTypeColor c : scolor) {
			ItemColor ite = itemColorDAO.find(item.getId(), c.getId());
			int ordinal = scolor.indexOf(c);
			if (ite == null) {
				ite = new ItemColor(item, c, ordinal);
				commonDAO.update(ite);
			} else if (ordinal != ite.getOrdinal()) {
				ite.setOrdinal(ordinal);
				commonDAO.update(ite);				
			}
		}
		List<ItemColor> savedColor = itemColorDAO.getItemColor(item.getId());
		for (ItemColor ite : savedColor) {
			if (!scolor.contains(ite.getColor())) {
				ite = (ItemColor) commonDAO.update(ite);
				try {
					int exec = commonDAO.delete(ite.getId(),ItemColor.class);	
//					for (KItemImage img : images) {
//						if (img.getColor().equals(ite)) {
//							img.setColor(null);
//							img = (KIImage) commonDAO.update(img);				
//						}
//					}
					if (exec < 1) {
						bad("Can not delete color:"+ite.getColor().getColor().getName_en());						
					}
				} catch (Exception e) {
					log.error("Can not delete ItemColor");
				}
			}
		}
		// SIZE
		for (ItemTypeSize c : ssize) {
			ItemSize ite = itemSizeDAO.find(item.getId(), c.getId());
			int ordinal = ssize.indexOf(c);
			if (ite == null) {
				ite = new ItemSize(item, c, ordinal);
				commonDAO.update(ite);
			} else if (ordinal != ite.getOrdinal()) {
				ite.setOrdinal(ordinal);
				commonDAO.update(ite);				
			}
		}
		List<ItemSize> savedSize = itemSizeDAO.getItemSize(item.getId());
		for (ItemSize ite : savedSize) {
			if (!ssize.contains(ite.getSize())) {
				ite = (ItemSize) commonDAO.update(ite);
				try {
					commonDAO.delete(ite);					
				} catch (Exception e) {
					log.error("Can not delete ItemSize");
				}
			}
		}
		// MATERIAL
		for (ItemTypeMaterial c : smaterial) {
			ItemMaterial ite = itemMaterialDAO.find(item.getId(), c.getId());
			int ordinal = smaterial.indexOf(c);
			if (ite == null) {
				ite = new ItemMaterial(item, c, ordinal);
				commonDAO.update(ite);
			} else if (ordinal != ite.getOrdinal()) {
				ite.setOrdinal(ordinal);
				commonDAO.update(ite);				
			}
		}
		List<ItemMaterial> saved = itemMaterialDAO.getItemMaterial(item.getId());
		for (ItemMaterial ite : saved) {
			if (!smaterial.contains(ite.getMaterial())) {
				ite = (ItemMaterial) commonDAO.update(ite);
				try {
					commonDAO.delete(ite);					
				} catch (Exception e) {
					log.error("Can not delete ItemMaterial");
				}
			}
		}
		loadData(item.getId());
		good();
	}
	
	public String back() {
		if (item == null || item.getId() <= 0) {
			bad(KConst.BAD_THING);
			return null;
		}
		KSelection kselection = (KSelection) Component.getInstance("kSelection");
		kselection.setItemId(item.getId());
		return "e_item";
	}
	
	public Item getItem() {
		return item;
	}

	public void setItem(Item item) {
		this.item = item;
	}

	public List<ItemTypeColor> getAcolor() {
		return acolor;
	}

	public void setAcolor(List<ItemTypeColor> acolor) {
		this.acolor = acolor;
	}

	public List<ItemTypeColor> getScolor() {
		return scolor;
	}

	public void setScolor(List<ItemTypeColor> scolor) {
		this.scolor = scolor;
	}

	public List<ItemTypeSize> getAsize() {
		return asize;
	}

	public void setAsize(List<ItemTypeSize> asize) {
		this.asize = asize;
	}

	public List<ItemTypeSize> getSsize() {
		return ssize;
	}

	public void setSsize(List<ItemTypeSize> ssize) {
		this.ssize = ssize;
	}

	public List<ItemTypeMaterial> getAmaterial() {
		return amaterial;
	}

	public void setAmaterial(List<ItemTypeMaterial> amaterial) {
		this.amaterial = amaterial;
	}

	public List<ItemTypeMaterial> getSmaterial() {
		return smaterial;
	}

	public void setSmaterial(List<ItemTypeMaterial> smaterial) {
		this.smaterial = smaterial;
	}

	public boolean isError() {
		return error;
	}

}
